﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.WebServices
{
    /// <summary>
    /// Summary description for SaveDataFromQuestionnaire
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class SaveDataFromQuestionnaire : System.Web.Services.WebService
    {
        [WebMethod()]
        public void SaveData(String name)
        {
            BSCEntities context = new BSCEntities();

            Survey_Form sf = new Survey_Form();



            string urldecoded = Server.UrlDecode(name);

            int i = 0;
            string questionPrefix = "Question[Question][" + i + "]";
            string question;

            while ((question = HttpUtility.ParseQueryString(urldecoded).Get(questionPrefix + "[TextBoxQuestion]")) != null)
            {
                Survey_Question sq;
                if (question != "")
                {
                    sq = new Survey_Question();
                    sq.Question = question;
                    sq.Order = i;
                    sq.Form_FK = 3;

                    context.AddToSurvey_Question(sq);
                    context.SaveChanges();

                    string answer;
                    int j = 0;
                    string answerPrefix = "[Answer][" + j + "]";
                    while ((answer = HttpUtility.ParseQueryString(urldecoded).Get(questionPrefix + answerPrefix + "[TextBoxAnswer]")) != null)
                    {
                        Survey_Answer sa;
                        if (answer != "")
                        {
                            sa = new Survey_Answer();
                            sa.Question_FK = sq.ID;
                            sa.Answer = answer;
                            sa.Order = j;

                            context.AddToSurvey_Answer(sa);
                            context.SaveChanges();
                        }

                        answerPrefix = "[Answer][" + ++j + "]";
                    }
                }
                questionPrefix = "Question[Question][" + ++i + "]";
            }
        }


        [WebMethod()]
        public string HelloToYou(string name)
        {
            return "Hello " + name;
        }
        [WebMethod()]
        public string sayHello()
        {
            return "hello ";
        }
    }
}
